//
//  ContactUsTableViewController.m
//  Kjs-iOS
//
//  Created by Pat on 16/8/5.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "ContactUsTableViewController.h"

@interface ContactUsTableViewController ()
@property (strong, nonatomic) UIWebView * webView;
@property (strong, nonatomic) NSMutableDictionary * rowHeight;
@end

@implementation ContactUsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"联系我们";
    self.tableView.allowsSelection = NO;
}

- (NSMutableDictionary *)rowHeight {
    if (!_rowHeight) {
        _rowHeight = [[NSMutableDictionary alloc] init];
    }
    return _rowHeight;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.briefIntro.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"info"];
    UILabel * title = [cell viewWithTag:1001];  //1001: title
    title.text = self.briefIntro[indexPath.row].title;
    
    self.webView = [cell viewWithTag:1002];  //1002: webview
    self.webView.tag = indexPath.row;
    self.webView.delegate = self;
    [self.webView loadHTMLString:self.briefIntro[indexPath.row].content baseURL:nil];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.rowHeight valueForKey:[NSString stringWithFormat:@"%ld", indexPath.row]] != nil) {
        CGFloat rowHeight = [[self.rowHeight valueForKey:[NSString stringWithFormat:@"%ld", indexPath.row]] floatValue];
        NSLog(@"%f", rowHeight);
        return rowHeight;
    }
    return UITableViewAutomaticDimension;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.tableView beginUpdates];
    CGFloat webViewHeight= [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"]floatValue];
    CGRect newFrame = webView.frame;
    newFrame.size.height = webViewHeight;
    webView.frame = newFrame;
//    webView.scrollView.scrollEnabled = NO;
    NSLog(@"rowh: %f", webViewHeight);
    [self.rowHeight setValue:[NSNumber numberWithFloat:webViewHeight] forKey:[NSString stringWithFormat:@"%ld", webView.tag]];
    [self.tableView endUpdates];
}
@end
